﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.PICCNursingUpperLimb
{
    public partial class UpperLimbPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        DateTime End;
        public UpperLimbPrints(PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            Start = StartDate;
            End = EndDate;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\PICC护理记录单(上肢）.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.UpperLimbNursing, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    Model.UpperLimb ps = XmlUtil.XMLToObject<Model.UpperLimb>(record1.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        "zhiguanjilu",
                        ps.ZhiGuanJiLu.PICCZhiGuanShiJian,
                        ps.ZhiGuanJiLu.PICCCaoZhe,
                        ps.ZhiGuanJiLu.ZhuShou,
                        ps.ZhiGuanJiLu.DaoGuanLeiXing,
                        ps.ZhiGuanJiLu.GuiGeXingHao,
                        ps.ZhiGuanJiLu.TiaoXingMaPiHao,
                        ps.ZhiGuanJiLu.DaoRuXiaoLeiXing,
                        ps.ZhiGuanJiLu.WeiXingDaoRuXiaoGuiGe,
                        ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiZuo.Equals("0") ? 1 : 0,
                        ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiYou.Equals("1") ? 1 : 0,
                        ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiZuo.Equals("0") ? 1 : 0,
                        ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiYou.Equals("1") ? 1 : 0,
                        ps.ZhiGuanJiLu.ChuanCiBuWeiQiTa,
                        ps.ZhiGuanJiLu.ChaRuChangDu,
                        ps.ZhiGuanJiLu.WaiLuChangDu,
                        ps.ZhiGuanJiLu.JianQuChangDu,
                        ps.ZhiGuanJiLu.XDingWei,
                        ps.ZhiGuanJiLu.ErCiDingWei,
                        ps.ZhiGuanJiLu.ZaiTuiChu,
                        ps.ZhiGuanJiLu.ShiJiChaRuChangDu,
                        ps.ZhiGuanJiLu.XinDeJianDuanWeiZhi,
                        ps.ZhiGuanJiLu.CaoZuoZhe,
                        ps.ZhiGuanJiLu.ZhiGunJingGuo,

                        "daiguanchuyuan",
                        ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[0],
                        ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[1],
                        ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[2],
                        ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[3],
                        ps.BaGuanJiLu.RiQi.Split('/')[0],
                        ps.BaGuanJiLu.RiQi.Split('/')[1],
                        ps.BaGuanJiLu.RiQi.Split('/')[2],
                        ps.BaGuanJiLu.RiQi.Split('/')[3],

                        ps.BaGuanJiLu.CaoZuoZhe,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[0].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[1].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[2].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[3].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[4].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[5].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[6].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[7].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[8].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[9].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[10].Equals("1") ? 1:0,
                        ps.BaGuanJiLu.BaGuanJingGuo,

                        record1.PatientName,
                        PatientInfo.SEX,//SEX
                        PatientInfo.AGE,//AGE
                        PatientInfo.NURSE_CELL_NAME,//NURSE_CELL_NAME,
                        PatientInfo.BED_NO,//BED_NO,
                        PatientInfo.PATIENT_ID//PATIENT_ID
                        );

                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("zhiguanjilu", typeof(string));
            dt.Columns.Add("PICCzhiguanshijian", typeof(string));
            dt.Columns.Add("PICCcaozuozhe", typeof(string));
            dt.Columns.Add("zhushou", typeof(string));
            dt.Columns.Add("daoguanleixing", typeof(string));
            dt.Columns.Add("guigexinghao", typeof(string));
            dt.Columns.Add("tiaoxingmapihao", typeof(string));
            dt.Columns.Add("daoruxueleixing", typeof(string));
            dt.Columns.Add("weixingdaoruxueguige", typeof(string));
            dt.Columns.Add("chauncishangzhizuo", typeof(string));
            dt.Columns.Add("chauncishangzhiyou", typeof(string));
            dt.Columns.Add("chauncixiazhizuo", typeof(string));
            dt.Columns.Add("chauncixiazhiyou", typeof(string));
            dt.Columns.Add("chaunciqita", typeof(string));
            dt.Columns.Add("charuchangdu", typeof(string));
            dt.Columns.Add("wailuchangdu", typeof(string));
            dt.Columns.Add("jianquchangdu", typeof(string));
            dt.Columns.Add("Xdingwei", typeof(string));
            dt.Columns.Add("ercidingwei", typeof(string));
            dt.Columns.Add("zaituichu", typeof(string));
            dt.Columns.Add("shijicharuchangdu", typeof(string));
            dt.Columns.Add("xindejianduanweizhi", typeof(string));
            dt.Columns.Add("caozuozhe", typeof(string));
            dt.Columns.Add("zhiguanjingguo", typeof(string));

            dt.Columns.Add("daiguanchuyuan", typeof(string));
            dt.Columns.Add("daiguanchuyuannian", typeof(string));
            dt.Columns.Add("daiguanchuyuanyue", typeof(string));
            dt.Columns.Add("daiguanchuyuanri", typeof(string));
            dt.Columns.Add("daiguanchuyuanshi", typeof(string));
            dt.Columns.Add("riqinian", typeof(string));
            dt.Columns.Add("riqiyue", typeof(string));
            dt.Columns.Add("riqiri", typeof(string));
            dt.Columns.Add("riqishi", typeof(string));

            dt.Columns.Add("baguancaozuozhe", typeof(string));
            dt.Columns.Add("baguanyuanyin1", typeof(string));
            dt.Columns.Add("baguanyuanyin2", typeof(string));
            dt.Columns.Add("baguanyuanyin3", typeof(string));
            dt.Columns.Add("baguanyuanyin4", typeof(string));
            dt.Columns.Add("baguanyuanyin5", typeof(string));
            dt.Columns.Add("baguanyuanyin6", typeof(string));
            dt.Columns.Add("baguanyuanyin7", typeof(string));
            dt.Columns.Add("baguanyuanyin8", typeof(string));
            dt.Columns.Add("baguanyuanyin9", typeof(string));
            dt.Columns.Add("baguanyuanyin10", typeof(string));
            dt.Columns.Add("baguanyuanyin11", typeof(string));
            dt.Columns.Add("baguanjingguo", typeof(string));

            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("sex", typeof(string));
            dt.Columns.Add("age", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void UpperLimbPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
